/*
 * Copyright 2011 Subhabrata Ghosh <subho.ghosh at langene.net>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



package net.langene.common;

//~--- non-JDK imports --------------------------------------------------------

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

import org.apache.commons.configuration.XMLConfiguration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Creates the Application Context, such as configuration, logging, etc.
 *
 * @author subhagho
 *
 */
public class Context {
    private static Logger	_logger = null;

    /*
     * Context Instance Section
     */
    private static final Context	_ctx = new Context();

    /*
     * End Context Instance Section
     */
    private XMLConfiguration	config = null;

    /**
     * Create the application context. - Load configuration, Initialize Logging
     *
     * @param configfile
     *            - Application configuration filename (XML)
     * @throws Exception
     */
    public static final void create(String configfile) throws Exception {
        _ctx.setup(configfile);
    }

    /**
     * Get a handle to the Context.
     * Assumes Context has been created.
     * 
     * @return Context Handle or NULL if context hasn't been created.
     */
    public static final Context get() {
        return _ctx;
    }

    private void setup(String configfile) throws Exception {
        config = new XMLConfiguration(configfile);

        /*
         * Setup the logging framework.
         */
        String	logconfigfile = config.getString("net.langene.log.config");

        if ((logconfigfile != null) &&!logconfigfile.isEmpty()) {
            String	logdir = config.getString("net.langene.log.directory");

            if ((logdir == null) || logdir.isEmpty()) {
                logdir = System.getProperty("java.io.tmpdir");
            }

            System.setProperty("LANGENE_LOGDIR", logdir);

            // Initialize the logging framework.
            // assume SLF4J is bound to logback in the current environment
            LoggerContext	context = (LoggerContext) LoggerFactory.getILoggerFactory();

            try {
                JoranConfigurator	configurator = new JoranConfigurator();

                configurator.setContext(context);
                context.reset();
                configurator.doConfigure(logconfigfile);
            } catch (JoranException je) {

                // StatusPrinter will handle this
            }

            StatusPrinter.printInCaseOfErrorsOrWarnings(context);
            _logger = LoggerFactory.getLogger("net.langene.nlp");
            _logger.info("Logger initialized...");
        }
    }

    /**
     * Get a handle to the application configuration.
     *
     * @return
     */
    public XMLConfiguration getConfig() {
        return config;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
